package com.biz.primus.management.promotion.tag;

import com.biz.primus.model.promotion.enums.PromotionGiftTypeEnum;
import java.io.IOException;
import java.util.Objects;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

/**
 * 促销活动赠品类型Jsp Tag
 *
 * @author david-liu
 */
@Setter
@Slf4j
public class PromotionGiftTypeTag extends TagSupport {
    private static final long serialVersionUID = -8519956736042240035L;

    private String selectedGiftType;

    private Boolean excludeProduct;

    @Override
    public int doStartTag() throws JspException {
        JspWriter jspWriter = pageContext.getOut();
        for (PromotionGiftTypeEnum promotionGiftType : PromotionGiftTypeEnum.values()) {
            String outHtml;
            try {

                if (Objects.nonNull(excludeProduct) && excludeProduct && PromotionGiftTypeEnum.PRODUCT == promotionGiftType) {
                    continue;
                }

                if (StringUtils.isNotBlank(selectedGiftType) && StringUtils.equalsIgnoreCase(promotionGiftType.name(), selectedGiftType)) {
                    outHtml = String.format("<option value=\"%s\" selected>%s</option>", promotionGiftType.name(), promotionGiftType.getDesc());
                } else {
                    outHtml = String.format("<option value=\"%s\">%s</option>", promotionGiftType.name(), promotionGiftType.getDesc());
                }
                jspWriter.println(outHtml);
            } catch (IOException e) {
                log.error("输出促销活动赠品类型jsp tag出错", e);
            }
        }
        return super.doStartTag();
    }
}
